package com.jiao.controller;

import com.jiao.bean.TBaoQuery;
import com.jiao.bean.vo.*;
import com.jiao.common.GlobalConstat;
import com.jiao.service.TBaoService;
import com.jiao.service.TTradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 参保情况和基金收入总查询
 */
@RestController
public class SynthesizeController {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    TTradeService tTradeService;
    @Autowired
    TTradeController tTradeController;

    @Autowired
    TBaoService tBaoService;

    @GetMapping(value="/all")
    JsonObjVo getInfo(@RequestParam int year, int month){
        //上月
        MonthCountVo lastMonth = getMonthCount(year, month - 1);

        //当月
        MonthCountVo currentMonth = getMonthCount(year, month);

        //当年 month 如果入参是0 的话，只计算年
        MonthCountVo yearMonth = getMonthCount(year,0);

        SynthesizeVo synthesizeVo = new SynthesizeVo();
        synthesizeVo.setLastMouth(lastMonth);
        synthesizeVo.setCurrentMouth(currentMonth);
        synthesizeVo.setCumulativeYear(yearMonth);
        return JsonObjVo.successReponse(synthesizeVo);
    }

    /**
     * 月情况总结
     * @param year
     * @param month
     * @return
     */
    private MonthCountVo getMonthCount(int year, int month) {
        MonthCountVo monthCountVo = new MonthCountVo();
        //全部
        TBaoQuery tBaoQuery = new TBaoQuery();
        if (month > 0) {
            tBaoQuery.createCriteria().andYearEqualTo(year).andMonthEqualTo(month);
        } else {
            tBaoQuery.createCriteria().andYearEqualTo(year);
        }
        int allInsured = tBaoService.countByTime(tBaoQuery);

        //在职
        TBaoQuery workQuery = new TBaoQuery();
        if (month > 0) {
            workQuery.createCriteria().andYearEqualTo(year)
                    .andStatusEqualTo(GlobalConstat.WorkStatus.working)
                    .andMonthEqualTo(month);
        } else {
            workQuery.createCriteria().andYearEqualTo(year)
                    .andStatusEqualTo(GlobalConstat.WorkStatus.working);
        }
        int workInsured = tBaoService.countByTime(workQuery);

        //退休
        TBaoQuery retireQuery = new TBaoQuery();

        if (month > 0) {
            retireQuery.createCriteria().andYearEqualTo(year)
                    .andStatusEqualTo(GlobalConstat.WorkStatus.retire)
                    .andMonthEqualTo(month);
        } else {
            retireQuery.createCriteria().andYearEqualTo(year)
                    .andStatusEqualTo(GlobalConstat.WorkStatus.retire);
        }
        int retireInsured = tBaoService.countByTime(retireQuery);
        TTradeVo tradeVo = tTradeController.getTotalTTrade(year, month);
        if (null != tradeVo) {
            monthCountVo.setFund(tradeVo);
        }
        // 参保人信息统计
        InsuredCount insuredCount = new InsuredCount();
        insuredCount.setCompanyCount(String.valueOf(allInsured));
        insuredCount.setIncumbencyCount(String.valueOf(workInsured));
        insuredCount.setRetireCount(String.valueOf(retireInsured));
        insuredCount.setPerCapitalPay("15万");
        insuredCount.setSocialInsuranceBase("2万");
        monthCountVo.setInsured(insuredCount);
        return monthCountVo;
    }


}
